package com.example.controller;


import com.example.pojo.dto.ContactorPageDTO;
import com.example.pojo.entity.Contactor;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.ContactorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/contactor")
@CrossOrigin(origins = "*")
public class ContactorController {

    @Autowired
    private ContactorService contactorService;

    /**
     *新增联系人
     * @param contactor
     * @return
     */
    @PostMapping("/add")
    public Result addContactor(@RequestBody Contactor contactor){
        log.info("新增联系人：{}",contactor);

        contactorService.save(contactor);

        return Result.success();
    }

    /**
     * 修改联系人
     * @return
     */
    @PutMapping("/update")
    public Result updateContactor(@RequestBody Contactor contactor){
        log.info("修改联系人：{}",contactor);
        contactorService.updateContactor(contactor);
        return Result.success();
    }

    /**
     * 删除联系人
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteContactor(@PathVariable Integer id){
        log.info("删除联系人：{}",id);
        contactorService.removeById(id);
        return Result.success();
    }

    /**
     * 查询联系人
     */
    @GetMapping("/get/{id}")
    public Result<Contactor> getContactor(@PathVariable Integer id){
        log.info("查找联系人:{}",id);
        Contactor contactor = contactorService.getById(id);
        return Result.success(contactor);
    }

    /**
     * 分页查找联系人
     */
    @GetMapping("/page")
    public Result<PageResult> page(ContactorPageDTO contactorPageDTO){
        log.info("分页数据:{}",contactorPageDTO);
        PageResult pageResult = contactorService.pageQuery(contactorPageDTO);
        return Result.success(pageResult);
    }
}
